package org.example.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 张琪
 * @date 2024/4/16 15:10
 */
public class ThreadTest {

    public static void main(String[] args) throws InterruptedException {
        final int threadSize = 1000;
        ThreadUnsafe threadUnsafe = new ThreadUnsafe();
        final CountDownLatch countDownLatch = new CountDownLatch(threadSize);
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < threadSize; i++) {
            executorService.execute(() -> {
                threadUnsafe.add();
                countDownLatch.countDown();
            });
        }
        // 中断当前线程, 直到线程数减为0/被其他线程中断await()方法
        countDownLatch.await();
        executorService.shutdown();
        System.out.println(threadUnsafe.get());

    }

}
